package com.google.code.gronono.jarseeker.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.google.code.gronono.jarseeker.model.ClassFile;
import com.google.code.gronono.jarseeker.model.JarSeeker;


public class JarSeekerController {
	private JarSeekerPanel jarSeekerPanel;
	private Action searchAction;
		
	public JarSeekerController() {
	}

	public JarSeekerPanel createPanel() {
		this.jarSeekerPanel = new JarSeekerPanel();
		initText();
		initTable();
		initBrowseBtn();
		initListeners();
		
		return this.jarSeekerPanel;
	}
	
	private void initTable() {
		JTable table = jarSeekerPanel.getResultTable();
		table.setDefaultRenderer(Object.class, new ToolTipTableCellRenderer());
		table.setCellSelectionEnabled(true);
	}

	private void initText() {
		jarSeekerPanel.getDirLabel().setText("Dossier :");
		try {
			jarSeekerPanel.getDirField().setText(new File(".").getCanonicalPath());
		} catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(jarSeekerPanel, "Le dossier courant n'est pas accessible.", "Erreur", JOptionPane.ERROR_MESSAGE);
		}
		jarSeekerPanel.getClassLabel().setText("Classe :");
		jarSeekerPanel.getBrowserBtn().setText("...");
	}
	
	private void initListeners() {
		KeyListener listener = new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				searchAction.setEnabled(
						!isEmpty(jarSeekerPanel.getDirField()) &&
						!isEmpty(jarSeekerPanel.getClassField()));
			}
		};
		jarSeekerPanel.getDirField().addKeyListener(listener);
		jarSeekerPanel.getClassField().addKeyListener(listener);
		
		searchAction = new AbstractAction("Rechercher") {
			/** serialVersionUID */
			private static final long serialVersionUID = -1116335047794109208L;

			public void actionPerformed(ActionEvent event) {
				JarSeeker seeker = new JarSeeker();
				try {
					List<ClassFile> classFiles = seeker.findJar(jarSeekerPanel.getDirField().getText(), jarSeekerPanel.getClassField().getText());
					jarSeekerPanel.getResultTable().setModel(new ResultTableModel("Classe", "JAR", classFiles));
				} catch (IOException e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(jarSeekerPanel, e.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
				}
			}
		};
		searchAction.setEnabled(false);
		jarSeekerPanel.getSearchBtn().setAction(searchAction);
		jarSeekerPanel.getDirField().addActionListener(searchAction);
		jarSeekerPanel.getClassField().addActionListener(searchAction);
	}
	
	private boolean isEmpty(JTextField textField) {
		String text = textField.getText();
		return text == null || "".equals(text.trim());
	}
	
	private void initBrowseBtn() {
		jarSeekerPanel.getBrowserBtn().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				fileChooser.setSelectedFile(new File(jarSeekerPanel.getDirField().getText()));
				int result = fileChooser.showOpenDialog(jarSeekerPanel);
				if (result == JFileChooser.APPROVE_OPTION) {
					jarSeekerPanel.getDirField().setText(fileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
	}
}
